﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace SuperStar.Models
{
    [MetadataType(typeof(BillMetaData))]
    public partial class Bill
    {
        protected class BillMetaData
        {
          
        }
        public int PointAmount { get; set; }
        public bool IsAllowedModifyPromotion { get; set; }
        public Bill()
        {

        }
    }

    public class BillModels
    {
        public SuperStarEntities DB { get; set; }
        public String DEBUGTYPE { get; set; }

        public BillModels()
        {
            DB = new SuperStarEntities();
        }


        public List<Bill> GetBills(Guid memberId)
        {
            var bills = from b in DB.Bills
                        where b.Customer_Id == memberId && b.Status_Id != 6 && b.Status_Id != 7
                        select b;
            var bList = bills.ToList();
            foreach (var bill in bList)
            {
                bill.IsAllowedModifyPromotion = bill.Status_Id == 1;
                bill.PointAmount = GetRemainPointPromotion(bill.Id);
            }
            return bList;
        }
        public int GetRemainPointPromotion(int billId)
        {
            var single = DB.spBill_GetRemainPointPromotion(billId).Single();
            return single != null ? single.Value : 0;
        }

        public Bill GetBill(int billId)
        {
            var bill = from b in DB.Bills
                       where b.Id == billId
                       select b;
            return bill.First();
        }
        public bool ChangeReseller(int billId,int resellerId)
        {
            return DB.spBill_ChangeReseller(billId, resellerId) > 0;
        }
        public bool AddToCart(int productId, Guid customerId)
        {
            if(DEBUGTYPE.Equals("2a_error"))
            {
                return DB.spTH02a_Bill_error_AddToCart_CancelBill(productId, customerId) > 0;
            }
            if (DEBUGTYPE.Equals("4a_error"))
            {
                return DB.spTH04a_Bill_error_AddToCart_CancelBill(productId, customerId) > 0;
            }
            if (DEBUGTYPE.Equals("4a_fix"))
            {
                return DB.spTH04a_Bill_fix_AddToCart_CancelBill(productId, customerId) > 0;
            }
            if (DEBUGTYPE.Equals("5_error"))
            {
                return DB.spTH05_Bill_error_AddToCart(productId, customerId) > 0;
            }
            if (DEBUGTYPE.Equals("5_fix"))
            {
                return DB.spTH05_Bill_fix_AddToCart(productId, customerId) > 0;
            }
            return DB.spBill_AddToCart(productId, customerId) > 0;
        }
        public bool CancelBill(int billId)
        {
            if (DEBUGTYPE.Equals("2b_error"))
            {
                return DB.spTH02b_Bill_error_CancelBill_AddToCart(billId) > 0;
            }
            if (DEBUGTYPE.Equals("2b_fix"))
            {
                return DB.spTH02b_Bill_fix_CancleBill_AddToCart(billId) > 0;
            }
            if (DEBUGTYPE.Equals("3_error"))
            {
                return DB.spTH03_Bill_error_CancelBill(billId) > 0;
            }
            if (DEBUGTYPE.Equals("3_fix"))
            {
                return DB.spTH03_Bill_fix_CancelBill(billId) > 0;
            }
            if (DEBUGTYPE.Equals("4b_error"))
            {
                return DB.spTH04b_Bill_error_AddToCart_CancelBill(billId) > 0;
            }
            return DB.spBill_CancelBIll(billId) > 0;
        }
        public List<Bill> GetBillsForReport(int month, int status)
        {
            return DB.spBill_GetBillsReport(month, status).ToList();
        } 
    }
}
